package com.txx.app.main.modules.web.function;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.rsq.function.txxpluginsdk.user.TXXCommonSPUtils;
import com.txx.app.main.App;
import com.txx.app.main.module.msg.bean.H5PlanbookParams;
import com.txx.app.main.modules.web.WebCommonFragment;
import com.txx.app.main.modules.web.js.H5BasePlugin;
import com.txx.app.main.modules.web.js.H5Event;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * created by houyl
 * on  6:51 PM
 */
public class H5GetPlanbookParams extends H5BasePlugin {

    @Override
    public boolean handleEvent(H5Event h5Event, WebCommonFragment fragment) {
        if (h5Event == null) {
            return false;
        }
        try {

            String json = h5Event.getJsonSource ();
            JSONObject jsonObject = new JSONObject (json);
            String callBackName = jsonObject.getString ("callBackName");
            //        处理后调用
            if (fragment != null) {
                fragment.getH5CallBackCommon (callBackName, new Gson ().toJson (getH5UserInfo ()));
            }
        } catch (JSONException e) {
            e.printStackTrace ();
        } catch (Exception e) {
            e.printStackTrace ();
        }

        return true;
    }

    @Override
    public void onRelease() {

    }

    @Override
    public String getCallBackName() {
        return null;
    }


    public H5PlanbookParams getH5UserInfo() {
        String channel = TextUtils.isEmpty(App.perInfo.getChannel()) ? "null" : App.perInfo.getChannel();
        String branchType = TextUtils.isEmpty(App.perInfo.getBranchType()) ? "null" : App.perInfo.getBranchType();
        String branchOffice = TextUtils.isEmpty(App.perInfo.getOrgCode()) ? "null" : App.perInfo.getOrgCode();
        final String agentInfo;
        TXXCommonSPUtils spUtils = new TXXCommonSPUtils(App.getContext(), "autoLogin");
        if (spUtils.getBoolean("yinbao_change_staffnumber")) {
            agentInfo = channel + "_" + branchType + "_1_" + branchOffice + "_null_null_null_" + App.perInfo.getReceiptno();
        } else {
            agentInfo = channel + "_" + branchType + "_0_" + branchOffice + "_null_null_null_" + App.perInfo.getStaffNumber();
        }
        H5PlanbookParams.MsgBean msgBean = new H5PlanbookParams.MsgBean();
        msgBean.setAgentInfo(agentInfo);
        H5PlanbookParams h5PlanbookParams = new H5PlanbookParams();
        h5PlanbookParams.setCode("0");
        h5PlanbookParams.setDesc("发送计划书参数成功");
        h5PlanbookParams.setMsg(msgBean);
        return h5PlanbookParams;
    }


}
